/**
 * 
 */
package com.pub.util;


/**
 * @author Administrator
 *
 */
public class StringUtils extends FilterUtils
{

	/**
	 * 
	 */
	public StringUtils() 
	{
	}

	/**
	 * @param args
	 */
	public static void main(String[] args) 
	{
		System.out.println("out === " + StringUtils.getDirByID(123456));
		
		try {
			//FileOutputStream f = new FileOutputStream("e:/t2/t1/ldmmyx.txt");
			/*File f = new File("e:/t2/t1/ldmmyx.txt");
			File parentDir = f.getParentFile();
			System.out.println("--1441--" + f.getName());
			
			System.out.println("--2221--" + parentDir.isDirectory());
			System.out.println("-3331--" + parentDir.exists());
			
			if(parentDir != null && !parentDir.exists()){
				System.out.println("--11--" + f.getParentFile());
				parentDir.mkdirs();
			}
			*/
//			FileUtils.copyFile(new File("e:/dq.txt"), "e:/t/t1/", null);
			
			//f.write(1000);
			//f.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		//f.mkdirs();
		
	}
	
	public static String splitAndFilterString(String input, int length) 
	{  
        if (input == null || input.trim().equals("")) {  
            return "";  
        }
        
        // delete all HTML tag  
        String str = input.replaceAll("\\&[a-zA-Z]{1,10};", "").replaceAll("<[^>]*>", "").replaceAll("[(/>)<]", "");  

        int len = str.length();
        
        if (len <= length) {  
            return str;  
        } else {  
            str = str.substring(0, length);  
            str += "...";  
        }
        
        return str;  
    }

	public static String Bytes2HexString(byte[] b) 
	{
		String ret = "";
		for (int i = 0; i < b.length; i++) {
			String hex = Integer.toHexString(b[i] & 0xFF);
			if (hex.length() == 1) {
				hex = '0' + hex;
			}
			ret += hex.toUpperCase();
		}
		return ret;
	}
	
	/** string cut, support UTF-8
	 * 
     * @param source 	cut string
     * @param limit		limit length. 
     * @param append	append string
     * 
     * @return after cut source string	
     */
	public static String cut(String source, int limit, String append)
    {
    	if(source == null)
    		return null;
    	
    	if(source.length() > limit){
    		StringBuilder sbCache;
    		if(append == null)
    			sbCache = new StringBuilder(limit);
    		else
    			sbCache = new StringBuilder(limit + append.length());
    		
    		String strCache;
          		
    		if(source.charAt(limit) != ' '){
    			if(source.lastIndexOf(' ', limit) == -1)
    				strCache = source.substring(0, limit).trim();
    			else
    				strCache = source.substring(0, source.lastIndexOf(' ', limit)).trim();
    		}else
    			strCache = source.substring(0, limit).trim();
    		
    		int iEndPos = strCache.length();

			for(int i = strCache.length() - 1; i >= 0; i--){
				if(Character.isLetterOrDigit(strCache.charAt(i))){
					break;
				}else
					iEndPos--;
			}

			if(iEndPos != strCache.length())
				strCache = strCache.substring(0, iEndPos);
			
			sbCache.append(strCache);
			if(append != null)
				sbCache.append(append);
   			
   			return sbCache.toString();
    	}else
    		return source;
    }
	
	/** add slashes into input string.
     * @param 	strInput 	input string
     * @param 	String 	
     */   	
   	public static String addcslashes(String strInput)
   	{
   		if(strInput == null )
   			return "";
   		else
   			return(strInput.replaceAll("\\\\'","'").replaceAll("\\\\\"","\"").replaceAll("\\\\","\\\\\\\\").replaceAll("\"","\\\\\"").replaceAll("'","\\\\'"));
	}

   	public static String getDirByID(int id)
   	{
   		String strID = Integer.toString(id);
   		int len = strID.length();
   		StringBuilder sbCache = new StringBuilder(20);
   		sbCache.append(CHAR_BACKSLASH);
   		sbCache.append(id % 10);
   		if(len < 2){
   			sbCache.append(CHAR_BACKSLASH);
   			sbCache.append("00");
   			sbCache.append(CHAR_BACKSLASH);
   			sbCache.append("0");
   			sbCache.append(id);
   			sbCache.append(CHAR_BACKSLASH);
   		}else if(len < 3){
   			sbCache.append(CHAR_BACKSLASH);
   			sbCache.append("00");
   			sbCache.append(CHAR_BACKSLASH);
   			sbCache.append(id);
   			sbCache.append(CHAR_BACKSLASH);
  		}else if(len < 4){
  			sbCache.append(CHAR_BACKSLASH);
   			sbCache.append("0");
   			sbCache.append(strID.substring(0, 1));
   			sbCache.append(CHAR_BACKSLASH);
   			sbCache.append(strID.substring(1, 3));
   			sbCache.append(CHAR_BACKSLASH);
   		}else{
   			sbCache.append(CHAR_BACKSLASH);
  			sbCache.append(strID.substring(len - 4, len - 2));
  			sbCache.append(CHAR_BACKSLASH);
   			sbCache.append(strID.substring(len - 2, len));
   			sbCache.append(CHAR_BACKSLASH);
   		}
    		
   		return sbCache.toString();
   	}
}
